// canvas 全局配置
var context = null;
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;
var app = getApp();
const pathUrl = app.globalData.pathUrl;
//注册页面
Page({
    canvasIdErrorCallback: function (e) {
        console.error(e.detail.errMsg)
    },
    //开始
    canvasStart: function (event) {
        isButtonDown = true;
        arrz.push(0);
        arrx.push(event.changedTouches[0].x);
        arry.push(event.changedTouches[0].y);

    },
    data: {
        height:"",
        imgSrc: "",
        orderNo:"",
        picUrlList: []
    },

    onLoad: function (e) {
        this.setData({
            orderNo: e.orderNo
        })
        this.getEleSignatureImg();
        var that = this;
        // 使用 wx.createContext 获取绘图上下文 context
        context = wx.createCanvasContext('canvas');
        context.beginPath()
        context.setStrokeStyle('#7aa6d1');
        context.setLineWidth(4);
        context.setLineCap('round');
        context.setLineJoin('round');
        context.draw();
        that.getSystemInfo();
    },
    getEleSignatureImg: function () {
        var that = this;
        wx.request({
            url: pathUrl + '/rest/fxjOrderMainController/attalist/' + that.data.orderNo,
            data: {
                attaType: 940,
                pageNumber: 1,
                pageSize: 100
            },
            success: function (res) {
                console.log(res);
                if (res.data.ok) {
                    that.setData({
                        picUrlList: res.data.data
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        })
    },
    
    // 获取设备高度
    getSystemInfo: function () {
        var that = this;
        wx.getSystemInfo({
            success: function (e) {
                console.log(e);
                that.setData({
                    height: (e.screenHeight) - 300,
                })
            }
        })
    },
    previewImage: function (e) {
        var that = this;
        var listIdx = e.currentTarget.dataset.listIdx;
        var imgIdx = e.currentTarget.dataset.imgIdx;
        var imgArr = that.data.picUrlList;
        var all = [];
        for (var index in imgArr) {
            all = all.concat(imgArr[index].attaUrl);
        }
        wx.previewImage({
            current: all[imgIdx], // 当前显示图片的http链接
            urls: all // 需要预览的图片http链接列表
        })
    },
    //删除图片
    deletePic: function (e) {
        var index = e.currentTarget.dataset.index;
        var deletPicId = e.currentTarget.dataset.picid;
        console.log(e);
        var that = this;
        if (that.data.flag) {
            that.data.picUrlList.splice(index, 1);
            that.setData({
                picUrlList: that.data.picUrlList,
                flag: false
            })
        }
        wx.request({
            url: pathUrl + '/rest/base/delfile/' + deletPicId,
            success: function (res) {
                if (res.data.ok || res.data.data) {
                    that.setData({
                        flag: true
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        })
    },

    //过程
    canvasMove: function (event) {
        var that = this
        if (isButtonDown) {
            arrz.push(1);
            // console.log(event)
            arrx.push(event.changedTouches[0].x);
            arry.push(event.changedTouches[0].y);
        };

        for (var i = 0; i < arrx.length; i++) {
            if (arrz[i] == 0) {
                context.moveTo(arrx[i], arry[i])
            } else {
                context.lineTo(arrx[i], arry[i])
            };

        };
        context.clearRect(0, 0, canvasw, canvash);
        context.setStrokeStyle('#7aa6d1');
        context.setLineWidth(4);
        context.setLineCap('round');
        context.setLineJoin('round');
        context.stroke();

        context.draw(false);
    },
    // 点击保存图片
    clickMe: function () {
        var that = this;
        if (!arrx.length || !arry.length || !arrz.length){
            wx.showModal({
                title: '提示',
                content: '请签名',
                showCancel: false,
            })
            return false;
        }
        wx.canvasToTempFilePath({
            canvasId: 'canvas',
            fileType: 'jpg',
            success: function (res) {
                wx.uploadFile({
                    url: pathUrl + '/rest/base/upload',
                    filePath: res.tempFilePath,
                    name: 'uploadfile_ant',
                    formData: {
                        sbtype: 'NO',
                        filetype: 940,
                        orderNo: that.data.orderNo
                    },
                    header: {
                        "Content-Type": "multipart/form-data"
                    },
                    success: function (res) {
                        if (res.statusCode == 200){
                            var imgObj = JSON.parse(res.data);
                            var picUrlList = imgObj[0].fileUrl
                            console.log(picUrlList)
                            wx.showModal({
                                title: '提示',
                                content: '上传成功',
                                showCancel: false,
                            })
                            console.log(that.data.picUrlList)
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta:-1
                                })
                            }, 1000);
                        }
                    },
                    fail: function (res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false
                        })
                    }
                });
            }
        })
    },
    canvasEnd: function (event) {
        isButtonDown = false;
    },
    cleardraw: function () {
        //清除画布
        arrx = [];
        arry = [];
        arrz = [];
        context.draw(false);
    },

})